package cn.com.infohold.smartcity.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import cn.com.infohold.smartcity.parent.ParentActivity;
import library.utils.Utils;

/**
 * Created by Mr.Fan on 2017/4/30.
 */

public class PageUtils {

    private static Map<Class<? extends Activity>, Serializable> pages = Collections.synchronizedMap(new HashMap<>());

    public static void jumpToPendingPage(Context context) {
        if (context == null || pages == null || pages.size() < 1)
            return;
        for (Map.Entry<Class<? extends Activity>, Serializable> entry : pages.entrySet()) {
            Class page = entry.getKey();
            Serializable data = entry.getValue();
            startActivity(context, page, data);
        }
        pages.clear();
    }

    public static void jumpToPageOrPend(Context context, Class<? extends Activity> page, Serializable data) {
        if (context == null)
            return;
        if (page != null) {
            if (Utils.isActivityRunning(context)) {
                Utils.moveApp2Front(context);
                startActivity(context, page, data);
                return;
            } else {
                pages.put(page, data);
            }
        }
        Utils.startAPP(context);
    }

    public static void startActivity(Context context, Class<? extends Activity> activity, Serializable data) {
        Intent intent = new Intent(context, activity);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Serializable.class.getName(), data);
        context.startActivity(intent);
        if (context instanceof ParentActivity) {
            ((ParentActivity) context).overridePendingTransitionFade();
        }
    }
}
